package com.sgb.businessmodule.view.widget;

import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.businessmodule.R;
import com.sgb.businessmodule.entity.PickFilterEntity;
import com.sgb.businessmodule.view.ui.adapter.PickFilterWindowAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;

/*
 * 省市区域联动筛选
 * 二级可多选
 * */
public class TwoLevelMultipleChoiceWindow extends BasePopWindow {

    private RecyclerView recycler1;
    private RecyclerView recycler2;
    private PickFilterWindowAdapter pickAdapter1;
    private PickFilterWindowAdapter pickAdapter2;

    //已经选中的数据集合
    private List<PickFilterEntity> selectedList;

    //回显
    private List<PickFilterEntity> historyList = new ArrayList<>();
    //回显
    private SparseBooleanArray checkList = new SparseBooleanArray();

    private List<PickFilterEntity<List<PickFilterEntity>>> list;
    private PickFilterEntity<List<PickFilterEntity>> currentHistoryFirstItem;
    private PickFilterEntity<List<PickFilterEntity>> currentFirstItem;
    private int currentPosition = 0;

    private PickFilterEntity all;


    private boolean flag = true;

    public TwoLevelMultipleChoiceWindow(Context context) {
        super(context);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_two_level_filter;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        recycler1 = holder.getPop().findViewById(R.id.rlv_list);
        recycler2 = holder.getPop().findViewById(R.id.rlv2_list);
        TextView reset = holder.getPop().findViewById(R.id.reset);
        TextView confirm = holder.getPop().findViewById(R.id.confirm);

        //重置
        reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setReset();
            }
        });

        //确定
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onResultClick != null) {
                    if (selectedList.size() > 0) { //点击
                        onResultClick.result(currentFirstItem, selectedList);
                        dissMiss();
                    } else {
                        onResultClick.result(currentFirstItem, pickAdapter2.getCheckDatas());
                        dissMiss();
                    }
                }
            }
        });

        if (pickAdapter1 == null) {
            pickAdapter1 = new PickFilterWindowAdapter(mContext, R.layout.popwindow_item_filter_child, null);
            pickAdapter1.setIsDisplay(false); //勾选隐藏
            pickAdapter1.setItemSelect(true);
            pickAdapter1.setDefItem(0);
        }
        if (pickAdapter2 == null) {
            pickAdapter2 = new PickFilterWindowAdapter(mContext, R.layout.popwindow_item_filter_child1, null);
            pickAdapter2.setIsDisplay(true); //勾选显示
            pickAdapter2.setMultiSelect(true);
            pickAdapter2.setMost(10);
            pickAdapter2.setDefItem(0);
        }

        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        recycler1.setLayoutManager(manager);
        recycler1.setAdapter(pickAdapter1);
        LinearLayoutManager manager1 = new LinearLayoutManager(mContext);
        recycler2.setLayoutManager(manager1);
        recycler2.setAdapter(pickAdapter2);

        pickAdapter1.setOnItemClick(new AdapterOnItemClick<PickFilterEntity>() {
            @Override
            public void onItemClick(PickFilterEntity entity, int position) {
                currentPosition = position;
                currentFirstItem = list.get(position);
                selectedList.clear();
                setData2(list.get(position).getChildren());
            }
        });

        pickAdapter2.setOnItemMultiClick(new BaseRecycleAdapter.OnItemMultiClick<PickFilterEntity>() {
            @Override
            public void onItemMultiClick(List<PickFilterEntity> pickFilterEntities, int position) {
                if (position == 0) {
                    selectedList.clear();
                    pickAdapter2.resetCheckDatas();
                    pickAdapter2.checkDatas(0);
                    flag = true;
                } else {
                    if (flag) {
                        pickAdapter2.resetCheckDatas();
                        pickAdapter2.checkDatas(position);
                        flag = false;
                    }
                    selectedList.clear();
                    selectedList.addAll(pickFilterEntities);
                }
            }
        });

        selectedList = new ArrayList<>();

        all = new PickFilterEntity("", "不限");
    }

    /**
     * 替换原有实例对象添加初始"不限"数据
     *
     * @param data 接口返回的省市二级联动数据
     */
    public void setData(List<PickFilterEntity<List<PickFilterEntity>>> data, int position, List<PickFilterEntity> secondList) {
        if (list == null) {
            list = new ArrayList<>();
            list.add(all);
            for (PickFilterEntity<List<PickFilterEntity>> entity : data) {
                PickFilterEntity<List<PickFilterEntity>> entityNew = new PickFilterEntity<>();
                entityNew.setNo(entity.getNo());
                entityNew.setName(entity.getName());
                List<PickFilterEntity> entityList = new ArrayList<>();
                entityList.add(all);
                for (PickFilterEntity entity1 : entity.getChildren()) {
                    PickFilterEntity entity2 = new PickFilterEntity();
                    entity2.setNo(entity1.getNo());
                    entity2.setName(entity1.getName());
                    entity2.setChildren(entity1.getChildren());
                    entityList.add(entity2);
                }
                entityNew.setChildren(entityList);
                list.add(entityNew);
            }

            //获取省份列表数据
            List<PickFilterEntity> entityList = new ArrayList<>();
            for (PickFilterEntity entity : list) {
                PickFilterEntity entity2 = new PickFilterEntity();
                entity2.setNo(entity.getNo());
                entity2.setName(entity.getName());
                entityList.add(entity2);
            }

            //填充省市列表
            pickAdapter1.setDatas(entityList);
            pickAdapter1.setDefItem(currentPosition);
        }

        if (position != -1) {
            currentPosition = position;
            pickAdapter1.setDefItem(currentPosition);
            recycler1.scrollToPosition(currentPosition);
        }

        setEchoData(secondList);
    }

    /**
     * 数据回显
     *
     * @param secondList
     */
    public void setEchoData(List<PickFilterEntity> secondList) {
        if (secondList != null && !secondList.isEmpty()) {
            historyList.clear();
            historyList.addAll(secondList);
            for (PickFilterEntity<List<PickFilterEntity>> item : list) {
                if (item.getChildren() != null && item.getChildren().contains(secondList.get(0))) {
                    currentHistoryFirstItem = item;
                    currentPosition = list.indexOf(item);
                    break;
                }
            }
        }

        currentFirstItem = list.get(currentPosition);
        pickAdapter1.setDefItem(currentPosition);
        setData2(list.get(currentPosition).getChildren());
    }

    /**
     * 设置市级区域列表
     *
     * @param list 市级列表数据
     */
    private void setData2(List<PickFilterEntity> list) {
        if (list == null) {
            pickAdapter2.resetCheckDatas();
            pickAdapter2.clearDatas();
            return;
        }
        pickAdapter2.setDatas(list);
        if (currentHistoryFirstItem != null) {
            if (currentHistoryFirstItem.equals(currentFirstItem)) {
                if (!historyList.isEmpty()) {
                    for (PickFilterEntity entity : list) {
                        if (historyList.contains(entity)) {
                            checkList.put(list.indexOf(entity), true);
                        } else {
                            checkList.put(list.indexOf(entity), false);
                        }
                    }
                    flag = false;
                    pickAdapter2.setCheckDefultDatas(historyList, checkList);
                }
                currentHistoryFirstItem = null;
            }
        } else {
            flag = true;
            pickAdapter2.resetCheckDatas();
            pickAdapter2.setCheckDefault(0);
        }
    }

    private void setReset() {
        currentPosition = 0;
        selectedList.clear();
        setData(list, currentPosition, null);
    }

    private OnResultClick onResultClick;

    public interface OnResultClick {

        void result(PickFilterEntity entity, List<PickFilterEntity> list);
    }

    public void setOnResultClick(OnResultClick onResultClick) {
        this.onResultClick = onResultClick;
    }

}
